<?php
include '../json.php';
function logWrite($msg)
{
	//echo $msg;
	$logDir = 'logs'.DIRECTORY_SEPARATOR.date('Ymd');		
	$file = $logDir.DIRECTORY_SEPARATOR.date("H").'.log';
	if(!file_exists($logDir)){
		mkdir($logDir,0777,1);
	}
	return file_put_contents($file,date("Y-m-d H:i:s")." ".getClientIp().':'.$msg." \r\n",FILE_APPEND);	
}

function getClientIp(){
	$ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    	$ip = $_SERVER["HTTP_CLIENT_IP"];
    }
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
		if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
        for ($i = 0; $i < count($ips); $i++) {
        	if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
        		$ip = $ips[$i];
            	break;
        	}
        }
    }
    $cip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
    return $cip;
}
function curlGetContents($url){
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $url); 
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 	
	$data = curl_exec($ch); 
	curl_close($ch);
	return $data;
}

function array_sort_by_filed($arr_data, $field, $descending = true)
{
	$arrSort = array();
	foreach ( $arr_data as $key => $value ) {
		$arrSort[$key] = $value[$field];
	}
		
	if( $descending ) {
		arsort($arrSort);
	} else {
		asort($arrSort);
	}
	$resultArr = array();
	foreach ($arrSort as $key => $value ) {
		$resultArr[] = $arr_data[$key];
	}
	return $resultArr;
}